/**
 * @file 03_unlink.c
 * @author your name (you@domain.com)
 * @brief 演示unlink函数的用法
 * @version 0.1
 * @date 2023-03-06
 *
 * @note 对普通文件，unlink的作用即是rm
 *
 * @copyright Copyright (c) 2023
 *
 */

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    {
        int fd = open("./a.tmp", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
        if (fd < 0)
        {
            perror("open");
            exit(-1);
        }
        else
        {
            printf("open ok, fd = %d\n", fd);
        }
        close(fd);
    }

    {
        int fd = open("./a.tmp", O_RDONLY);
        if (fd < 0)
        {
            perror("open");
            exit(-1);
        }
        else
        {
            printf("open ok, fd = %d\n", fd);
        }
        close(fd);
    }

    unlink("./a.tmp");

    {
        int fd = open("./a.tmp", O_RDONLY);
        if (fd < 0)
        {
            perror("open");
            exit(-1);
        }
        else
        {
            printf("open ok, fd = %d\n", fd);
        }
        close(fd);
    }
}
